﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using umbraco.cms.businesslogic.utilities;
using System.Drawing;

namespace tswe.umbraco.languageEditor.classes
{
    public class Helpers
    {
        private static int[] DiffCharCodes(string aText, bool ignoreCase)
        {
            int[] Codes;

            if (ignoreCase)
                aText = aText.ToUpperInvariant();

            Codes = new int[aText.Length];

            for (int n = 0; n < aText.Length; n++)
                Codes[n] = (int)aText[n];

            return (Codes);
        }

        public static void Diff2RichTextBox(string a_line, string b_line, ref RichTextBox rtb)
        {
            rtb.ReadOnly = false;
            rtb.Clear();
            SetDefaultStyle(rtb);

            int[] a_codes = DiffCharCodes(a_line, false);
            int[] b_codes = DiffCharCodes(b_line, false);
            string text = String.Empty;

            Diff.Item[] diffs = Diff.DiffInt(a_codes, b_codes);

            int pos = 0;
            for (int n = 0; n < diffs.Length; n++)
            {
                Diff.Item it = diffs[n];

                // write unchanged chars
                while ((pos < it.StartB) && (pos < b_line.Length))
                {
                    text += b_line[pos];
                    pos++;
                }
                rtb.SelectedText = text;
                text = String.Empty;

                // write deleted chars
                if (it.deletedA > 0)
                {
                    SetDeleteStyle(rtb);

                    for (int m = 0; m < it.deletedA; m++)
                    {
                        text += a_line[it.StartA + m];
                    }
                    rtb.SelectedText = text;
                    text = String.Empty;

                    SetDefaultStyle(rtb);
                }

                // write inserted chars
                if (pos < it.StartB + it.insertedB)
                {
                    SetInsertStyle(rtb);
                   
                    while (pos < it.StartB + it.insertedB)
                    {
                        text += b_line[pos];
                        pos++;
                    }
                    rtb.SelectedText = text;
                    text = String.Empty;

                    SetDefaultStyle(rtb);
                }
            } 

            // write rest of unchanged chars
            while (pos < b_line.Length)
            {
                text += b_line[pos];
                pos++;
            }
            rtb.SelectedText = text;
            rtb.ReadOnly = true;
        }

        private static void SetDeleteStyle(RichTextBox rtb)
        {
            rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
            rtb.SelectionColor = Color.Red;
            rtb.SelectionBackColor = Color.LightPink;
        }

        private static void SetInsertStyle(RichTextBox rtb)
        {
            rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Bold);
            rtb.SelectionColor = Color.Green;
            rtb.SelectionBackColor = Color.LightGreen;
        }

        private static void SetDefaultStyle(RichTextBox rtb)
        {
            rtb.SelectionFont = new Font("Courier New", 10, FontStyle.Regular);
            rtb.SelectionColor = Color.Black;
            rtb.SelectionBackColor = Color.LightGray;
        }

    }
}
